package cn.gcyl.module.biz.service.meetingplan;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.meetingplan.vo.*;
import cn.gcyl.module.biz.dal.dataobject.meetingplan.MeetingPlanDO;
import cn.gcyl.module.biz.dal.mysql.meetingplan.MeetingPlanMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.MEETING_PLAN_NOT_EXISTS;

/**
 * 参会方案 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MeetingPlanServiceImpl implements MeetingPlanService {

    @Resource
    private MeetingPlanMapper meetingPlanMapper;

    @Override
    public Integer createMeetingPlan(MeetingPlanSaveReqVO createReqVO) {
        // 插入
        MeetingPlanDO meetingPlan = BeanUtils.toBean(createReqVO, MeetingPlanDO.class);
        meetingPlanMapper.insert(meetingPlan);

        // 返回
        return meetingPlan.getId();
    }

    @Override
    public void updateMeetingPlan(MeetingPlanSaveReqVO updateReqVO) {
        // 校验存在
        validateMeetingPlanExists(updateReqVO.getId());
        // 更新
        MeetingPlanDO updateObj = BeanUtils.toBean(updateReqVO, MeetingPlanDO.class);
        meetingPlanMapper.updateById(updateObj);
    }

    @Override
    public void deleteMeetingPlan(Integer id) {
        // 校验存在
        validateMeetingPlanExists(id);
        // 删除
        meetingPlanMapper.deleteById(id);
    }

    @Override
        public void deleteMeetingPlanListByIds(List<Integer> ids) {
        // 删除
        meetingPlanMapper.deleteByIds(ids);
        }


    private void validateMeetingPlanExists(Integer id) {
        if (meetingPlanMapper.selectById(id) == null) {
            throw exception(MEETING_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public MeetingPlanDO getMeetingPlan(Integer id) {
        return meetingPlanMapper.selectById(id);
    }

    @Override
    public PageResult<MeetingPlanDO> getMeetingPlanPage(MeetingPlanPageReqVO pageReqVO) {
        return meetingPlanMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MeetingPlanDO> getMeetingPlanByIds(List<Long> meetingPlanIds) {
        // 添加空值检查
        if (meetingPlanIds == null || meetingPlanIds.isEmpty()) {
            return new ArrayList<>(); // 返回空列表而不是null
        }
        return meetingPlanMapper.selectByIds(meetingPlanIds);
    }

}